package com.jili.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jili.blog.dao.entity.Article;
import com.jili.blog.dao.mapper.ArticleMapper;
import com.jili.blog.service.ArticleService;
import com.jili.blog.service.SysUserService;
import com.jili.blog.service.TagService;
import com.jili.blog.vo.ArticleVo;
import com.jili.blog.vo.Result;
import com.jili.blog.vo.params.PageParams;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private TagService tagService;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public Result listArticle(PageParams params) {
        Page<Article> page=new Page<>(params.getPage(),params.getPageSize());
        LambdaQueryWrapper<Article>queryWrapper=new LambdaQueryWrapper<>();
        //置顶排序
        queryWrapper.orderByDesc(Article::getWeight);
        //时间排序
        queryWrapper.orderByDesc(Article::getCreateDate);
        List<Article> records = articleMapper.selectPage(page, queryWrapper).getRecords();

        List<ArticleVo>articleVoList=copyList(records);
        return Result.success(articleVoList);
    }

    @Override
    public Result hotArticle(int limit) {
        LambdaQueryWrapper<Article>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Article::getViewCounts);
        queryWrapper.select(Article::getId,Article::getTitle);
        //limit字符串后面要加空格！
        queryWrapper.last("limit "+limit);
        List<Article> records = articleMapper.selectList(queryWrapper);
        return Result.success(records);
    }

    @Override
    public Result newArticle(int limit) {
        LambdaQueryWrapper<Article>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Article::getCreateDate);
        queryWrapper.select(Article::getId,Article::getTitle,Article::getCreateDate);
        //limit字符串后面要加空格！
        queryWrapper.last("limit "+limit);
        List<Article> records = articleMapper.selectList(queryWrapper);
        return Result.success(records);
    }

    @Override
    public Result listArchives() {
        return Result.success(articleMapper.findArchives());
    }

    private List<ArticleVo> copyList(List<Article> records){
        List<ArticleVo> articleVoList=new ArrayList<>();
        for(Article article: records){
            articleVoList.add(copy(article,true,true));
        }
        return articleVoList;
    }

    private ArticleVo copy(Article article,boolean isTag,boolean isAuthor) {
        ArticleVo articleVo=new ArticleVo();
        BeanUtils.copyProperties(article,articleVo);
        articleVo.setCreateDate(new DateTime(articleVo.getCreateDate()).toString("yyyy-MM-dd HH:mm:ss"));
        if(isTag){
            Long articleId = article.getId();
            articleVo.setTags(tagService.findTagsByArticleId(articleId));
        }
        if(isAuthor){
            Long authorId = article.getAuthorId();
            articleVo.setAuthor(sysUserService.findUserById(authorId).getNickname());
        }
        return articleVo;
    }
}
